package pgpchat;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.security.PublicKey;

import pgpframework.Message.PGPMessage;

public class PGPHelper {

	public static void sendKey(PublicKey key, Socket receiver) {
		ObjectOutputStream outputStream;
		try {
			outputStream = new ObjectOutputStream(receiver.getOutputStream());
			System.out.println("Enviando chave publica");
			outputStream.writeObject(key);

			System.out.println("Chave enviada: " + key.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static PublicKey receivePubKey(Socket emitent) {
		ObjectInputStream inStream;
		try {
			inStream = new ObjectInputStream(emitent.getInputStream());
			PublicKey key = (PublicKey) inStream.readObject();
			System.out.println("Recebeu chave publica");
			System.out.println("Chave recebida: " + key.toString());
			return key;
		} catch (IOException | ClassNotFoundException e) {
			System.out.println("Não recebeu chave publica");
		}
		return null;
	}

	public static void sendMsg(PGPMessage msg, Socket receiver) {
		ObjectOutputStream outputStream;
		try {
			outputStream = new ObjectOutputStream(receiver.getOutputStream());
			System.out.println("Enviando msg PGP");
			outputStream.writeObject(msg);
			System.out.println("Chave enviada: " + msg.getConf().toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static PGPMessage receivePGPMsg(Socket emitent) {
		ObjectInputStream inStream;
		try {
			inStream = new ObjectInputStream(emitent.getInputStream());
			PGPMessage msg = (PGPMessage) inStream.readObject();
			System.out.println("Recebeu msg PGP");
			System.out.println("Descriptogrando: " + msg.toString());
			return msg;
		} catch (IOException | ClassNotFoundException e) {
			System.out.println("Não recebeu msg");
		}
		return null;
	}
}
